<?php 
Class wxmenuController extends appController
{
	/**
	 * 显示菜单列表
	 */
	public function index()
	{
		$wxmu = Model('wxmenu');	
		$local_menu = $wxmu->get_wxmenu();
		$menu = $local_menu;
		$this->assign('view_menu', $menu);
		$this->display('index.html');
	}
	
	public function toggle()
	{
		$result=array('error'=>0,'content'=>'');
		$cfg['status_num']=intval($_POST['status_num']);
		$cfg['id']=intval($_POST['id']);
		$cfg['table']='wxmenu';
		$cfg['name']=$_POST['name'];
		if(Model('app')->toggle($cfg))
		{
			$result['error']=0;
		}
		else
		{
			
		}
		print_r(json_encode($result));
		exit;
	
	}
	
	/**
	 * 菜单排序
	 */
	public function ordering()
	{
		$this->admin_priv('ord_wx_menu');
		$id = yf_empty(intval($_POST['id']), '', true);
		$val = yf_empty(intval($_POST['val']), '', true);
		$result = Model('wxmenu')->ordering($val, $id);
		$this->showMsg($result);
	}
	
	/**
	 * 新建菜单
	 */
	public function new_main_menu()
	{
		$this->admin_priv('add_wx_menu');
		$menu = $_POST;
		unset($menu['old_pid']);
		$wm = Model('wxmenu');
		$result = array('error' => '1', 'content' => '');
		$menu['name'] = yf_empty($menu['name'], "菜单名不能为空");	
		
		if($menu['parent_id'] == 0)
		{
			if(count($wm->get_parent_menu()) > 2)
			{
				$result['content'] = '根目录菜单不能超过三个';
				$this->showMsg($result);
			}
		}
		else
		{
			if(count($wm->get_child_menu($menu['parent_id'])) > 4)
			{
				$result['content'] = '子目录菜单不能超过五个';
				$this->showMsg($result);
			}
		}
		
		if($menu['type'] == 1)
		{
			unset($menu['rule']);
		}
		else 
		{
			unset($menu['url']);
		}
		unset($menu['id']);
		$res = $wm->insert($menu,'wxmenu');
		if($res)
		{
			$result['error']=0;
			$result['content']=Lang('SUCCESS_ADD');
		}
		else 
		{
			$result['content']=Lang('INVALID_OPERATION');
		}
		$this->showMsg($result);
	}
	
	//显示添加按钮
	function add_main_menu()
	{
		$result=array('error'=>'0','data'=>'','cat'=>'');
		$wr=Model('wxrule');
		$wm=Model('wxmenu');
		$result['cat']=$wm->get_wxmenu(array('parent_id'=>0));
	
		//获取规则列表
		$rule_list=$wr->get_rule_list();
		if($rule_list)
		{
			foreach( $rule_list as $k=>$v)
			{
					
				$temp_list.="<option value='".$v['id']."'>".$v['name']."</option>";
					
			}
		}
		$result['data']=$temp_list;
		die(json_encode($result));
		
	}
	
	/**
	 * 删除本地菜单
	 */
	public function del_btn()
	{
		$this->admin_priv('del_wx_menu');
		$result = array('error' => 1, 'content' => '删除失败');
		$id = yf_empty($_REQUEST['id'], '', true);
		$res = Model('wxmenu')->del(array('id' => $id, 'table' => 'wxmenu'));
		if($res)
		{
			$result['error'] = 0;
			$result['content'] = "删除成功";
		}
		$this->showMsg($result);
	}
	
	/**
	 * 删除微信数据库上的
	 */
	public function del_menu()
	{
		$this->admin_priv('del_wx_menu', 2);
		$res = array('error' => 0, 'content' => '删除成功');
		$token= Model('wxmenu')->getAccessToken();
		$wx = new weixinController();
		$result = json_decode($wx->del_menu($token));
		$this->showMsg($res);
	}
	
	/**
	 * 更新数据库中的菜单
	 */
	public function refresh_menu()
	{
		$this->admin_priv('ud_wx_menu');
		$result = array('error' => 1, 'content' => '');
		$wxmu = Model('wxmenu');
		$wx = new weixinController();
		$token = $wxmu->getAccessToken();
		$wxmenu = $wx->get_menu($token);
		if($wxmenu)
		{
			$menu = (array)json_decode($wxmenu);
			$menu = $this->deformat_menu($menu);
		   	if($wxmu->del(array('table' => 'wxmenu')))//清空数据库
		  	{
				$res = $wxmu->insert_menu($menu);
				if($res)
				{
					$result['error']=0;
					$result['content']='刷新成功';
				}
				else
				{
					$result['content']=Lang('INVALID_OPERATION');
				}
		   	}
		   	else 
		   	{
		     	$result['content']='删除数据库失败';
		   	}
		}
		else 
		{
			$result['content']='获取服务器菜单失败';
		}
		$this->showMsg($result);
	}
	
	/**
	 * 更新菜单信息
	 */
	public function update_menu()
	{
		$this->admin_priv('edit_wx_menu');
		$menu = isset($_POST) ? $_POST : '';
		$result = array('error' => 1, 'content' => '');
		$wm = Model('wxmenu');
		
		if(empty($menu['name']))
		{
			$result['content'] = Lang('NAME_EMPTY');
			$this->showMsg($result);
		}
		
		$old_pid = $menu['old_pid'];
		unset($menu['old_pid']);
		
		if($old_pid != $menu['parent_id'])
		{
		  	if($menu['parent_id'] == 0)
		 	{
				if(count($wm->get_parent_menu()) > 2)
				{
					$result['content']='根目录菜单不能超过三个';
					$this->showMsg($result);
				}
		 	}
		 	else
		 	{
				if(count($wm->get_child_menu($menu['parent_id'])) > 4)
				{
					$result['content']='子目录菜单不能超过五个';
					$this->showMsg($result);
				}
		 	}
		}
		
		if($menu['type']==1)
		{
			unset($menu['rule']);
		}
		else
		{
			unset($menu['url']);
		}
		
		$id = $menu['id'];
		unset($menu['id']);
		$res = $wm->update($menu, array('id' => $id, 'table' => 'wxmenu'));
		if($res)
		{
			$result['error']=0;
			$result['content']=Lang('SUCCESS_edit');
		}
		else
		{
			$result['content']=Lang('INVALID_OPERATION');
		}
		$this->showMsg($result);
	}
	
	/**
	 * 显示添加菜单页面
	 */
	public function add_menu()
	{
		$this->admin_priv('add_wx_menu', 2);
		$wm = Model('wxmenu');
		//父类列表
		$menu_list = $wm->get_parent_menu();
		//规则列表
		$rule_list = Model('wxrule')->get_rule_list();
		$this->assign('rule_list',  $rule_list);
		$this->assign('cat_list',  $menu_list);
		$this->assign("action", "new_main_menu");
		$this->display('wxmenu.html');
	}
	
	/**
	 * 显示菜单编辑页面
	 */
	public function edit_menu()
	{
		$this->admin_priv('edit_wx_menu', 2);
		$id = yf_empty(intval($_GET['id']), '', true);
		$wm = Model('wxmenu');
		$wxmenu = $wm->get_wxmenu_info($id);
		//父类列表
		$menu_list = $wm->get_parent_menu();
		
		//规则列表
		$rule_list = Model('wxrule')->get_rule_list();
		
		$this->assign('rule_list',  $rule_list);
		$this->assign('cat_list',  $menu_list);
		$this->assign('wxmenu',  $wxmenu);
		$this->assign("action", "update_menu");
		$this->display('wxmenu.html');
		
	}

	/**
	 * 提交菜单
	 */
	public function sumbit_menu()
	{
		$this->admin_priv('set_wx_menu', 2);
		$res = array('error' => 0, 'content' => '');
		$wx = new weixinController();
		$wxmu = Model('wxmenu');
		$token = $wxmu->getAccessToken();
		$button = array();
		$data = array();
		$menu = array();
		$json_result = array('error' => '0', 'content' => '');
		$cfg['is_show'] = 1;
		$temp = $wxmu->get_wxmenu($cfg);
		$temp = $this->format_menu($temp);
		
		$result = json_decode($wx->create_menu($temp, $token));
		if($result->errcode != 0)
		{
			$res['error']=1;
		}
		
		$res['content'] = $result->errmsg;
		$this->showMsg($res);
	}

	private function deformat_menu($menu)
	{
		$menu = (array)$menu['menu'];
		$menu = (array)$menu['button'];
		$count = 0;
		$new_menu = array();	
		foreach($menu as $k => $v)
		{
			$menu[$k] = (array)$v;
		
			if($v->sub_button)
			{
				foreach($v->sub_button as $kk => $vv)
				{
					$menu[$k]['sub_button'][$kk] = (array)$vv;
					if($vv->type == 'click')
					{
					   	$menu[$k]['sub_button'][$kk]['type'] = 2;
					   	$menu[$k]['sub_button'][$kk]['rule'] = str_replace('event_', '', $menu[$k]['sub_button'][$kk]['key']);
					   	unset($menu[$k]['sub_button'][$kk]['key']);
					}
					else 
					{
						$menu[$k]['sub_button'][$kk]['type'] = 1;
					}
				}
			}
			if($v->type == 'click')
			{
				$menu[$k]['type'] = 2;
			    $menu[$k]['rule'] = str_replace('event_', '', $menu[$k]['key']);
			    unset($menu[$k]['key']);
			}
			else 
			{
				$menu[$k]['type'] = 1;
			}
		}
		return $menu;
	}
	
	private function format_menu($temp)
	{
		$button = array();
		$menu = array();
		foreach($temp as $k => $v)
		{
			$menu[$k]['name'] = urlencode($v['name']);
			if($v['type'] == 1)
			{
				$menu[$k]['type'] = 'view';
				$menu[$k]['url'] = $v['url'];
			}
			else 
			{
				$menu[$k]['type'] = 'click';
				$menu[$k]['key'] = "event_".$v['rule'];
			}
			
			if($v['sub_button'])
			{
				foreach($v['sub_button'] as $kk => $vv)
				{
				    $menu[$k]['sub_button'][$kk]['name'] = urlencode($vv['name']);
			     	if($vv['type'] == 1)
			     	{
				    	$menu[$k]['sub_button'][$kk]['type'] = 'view';
				   		$menu[$k]['sub_button'][$kk]['url'] = $vv['url'];
			    	}
			     	else 
			    	{
				   		$menu[$k]['sub_button'][$kk]['type'] = 'click';
				   		$menu[$k]['sub_button'][$kk]['key'] = "event_".$vv['rule'];
					}
				}
			}
		}
		$button['button'] = $menu;
		return urldecode(json_encode($button));
	}
	
	
}